﻿#include "stdafx.h"
#include "ScriptMapleZk.h"
#include "script.h"
#include "GameAppDlg.h"

CScriptMapleZk::CScriptMapleZk()
{
}


CScriptMapleZk::~CScriptMapleZk()
{
}
//开始工作
void CScriptMapleZk::DoWork()
{
	if (this->LoginOpenClient()) {
		if (this->LoginGame(true)) {
			TaskRun();
		}
	}
}
//移动进入祭坛入口
void CScriptMapleZk::MobileAltar()
{
	
	if (MovetoX( 140)) {
		if (!m_dm->RunScript(L"选择进入祭坛")) {//不存在选择祭坛进行移动
			m_dm->KeyDownChar(L"right");
			Sleep(50);
			for (size_t i = 0; i < 20; i++)
			{
				m_dm->KeyPress(38);
				if (m_dm->RunScript(L"选择进入祭坛")) {
					break;
				}
			}
			m_dm->KeyUpChar(L"right");
			Sleep(RandomNumber(6000, 7000));
			if (m_dm->RunScript(L"扎昆祭台入口")) {
				return;
			}
			else {
				m_dm->KeyDownChar(L"left");
				for (size_t i = 0; i < 20; i++)
				{
					m_dm->KeyPress(38);
					if (m_dm->RunScript(L"选择进入祭坛")) {
						break;
					}
				}
				Sleep(RandomNumber(500, 1000));
				m_dm->KeyUpChar(L"left");
			}
		}

	}
}

//运行boss任务
void CScriptMapleZk::TaskRun()
{

	CString strList = L"关闭广告|关闭广告1|关闭广告2|登录确定";
	long x, y, z = 0;
	long count = 0, j = 0;
	bool bIsLine = false; //是否切换线路
	bool bIsTeam = false; //进入地图后组队是否变更
	int nState = 0;
	bool bZheng = false;
	bool bStamp = false;
	int uNum = 0;
	while (m_Flag)
	{
		RunScriptList(strList);
		if (m_State == 0) {
			if (m_dm->RunScript(L"交易地图")) {
				m_dm->KeyPressChar(L"up");
			}
			//退出564地图
			ExitWlsMap();
			//退出自由市场
			ExitShiChang();
			//到达村庄
			if (m_dm->RunScript(L"村庄地图")) {
				//uNum = 0;
				ChoseBoss(-5, 70);
				if(m_State==9) continue;
			}
			if (m_dm->RunScript(L"扎昆入口")) {
				//uNum = 0;
				MobileAltar();
			}
			if (m_dm->RunScript(L"扎昆祭台入口")) {
				//uNum = 0;
				//对话npc //如果已刷变更状态
				AddTeam();
				CString strXue = m_dm->FindImgEx(L"血条");
				if (strXue!=L"" &&  strXue.Find(L"|") ==-1) {//存在组队血条
					ScriptDelay(m_DmIndex, RandomNumber(130, 250));
					if (m_dm->RunScript(L"导航窗口")) {
						m_dm->KeyPressChar(L"p");
					}
					if (m_dm->RunScript(L"npc啊杜比斯")) {
						for (size_t i = 0; i < 10; i++)//循环查找下一步
						{

							if (m_dm->RunScript(L"下一步")) {
								m_dm->KeyPressChar(L"space");
								ScriptDelay(m_DmIndex, RandomNumber(150, 250));
								if (m_dm->RunScript(L"扎昆已刷")) {
									m_State = 9;
									break;
								}
							}
							if (m_dm->RunScript(L"进入扎昆")) {
								m_dm->KeyPressChar(L"enter");
							}
						}
						if (m_State == 9) continue;
					}
				}
			}
			if (m_dm->RunScript(L"扎昆祭台地图")) {
				m_State = 6;
				nState = 0;
			}
			
			if (uNum > 200 && uNum != -1) {
				ChoseBoss(-5, 70);
				uNum = -1;
			}
			uNum++;
		}
		else if (m_State == 6) {//进入扎昆Boss
			if (m_dm->RunScript(L"扎昆祭台入口")) {
				nState = 6;
			}
			if (nState == 0) {//开启技能键
				ScriptDelay(m_DmIndex, RandomNumber(500, 1000));
				m_dm->KeyPressChar(L"1");
				ScriptDelay(m_DmIndex, RandomNumber(4000, 4500));
				nState = 1;
			}
			else if (nState == 1) {
				m_dm->KeyDownChar(L"right");
				ScriptDelay(m_DmIndex, RandomNumber(3000, 3200));
				m_dm->KeyUpChar(L"right");
				ScriptDelay(m_DmIndex, RandomNumber(1000, 1550));
				nState = 2;

			}
			else if (nState == 2) {//定位退出npc
				if (m_dm->RunScript(L"npc阿们")) {
					m_dm->KeyDownChar(L"left");
					//Sleep(2500,3000);
					Sleep(RandomNumber(1500, 1600));
					m_dm->KeyUpChar(L"left");
					nState = 3;
					bZheng = false; //设定为未背包未整理
				}
				++z;
				if (z > 50) {
					nState = 1;
					z = 0;
				}
			}
			else if (nState == 3) {
				if (m_dm->RunScript(L"背包线", &x, &y)) {

					//整理背包
					if (!bZheng) {
						bZheng = ZhengLi();
					}
					m_dm->RunScript(L"背包缩小");
					if (m_dm->RunScript(L"缩小", 0) || m_dm->RunScript(L"整理", 0)) {

						//待增加处理 选中红球栏目 +++
						if (m_dm->RunScript(L"红球", &x, &y)) {
							g_main_cwnd->AppendText(L"找到红球");
							m_dm->MoveToEx(x + 10, y + 10, 10, 10);
							Sleep(RandomNumber(100, 150));
							m_dm->LeftClick();
							Sleep(RandomNumber(800, 1000));
							m_dm->MoveTo(RandomNumber(500, 600), RandomNumber(350, 450));
							Sleep(RandomNumber(800, 1000));
							m_dm->LeftClick();
							CString strNum;
							for (size_t i = 0; i < 10; i++)
							{
								int nColNum = m_dm->GetColorNum(200, 569, 814, 670, L"e0cfaa-050505", 0.95);
								if (nColNum > 800) {
									strNum.Format(L"颜色数量:%d", nColNum);
									g_main_cwnd->AppendText(strNum);
									m_dm->KeyPress(73);
									Sleep(200);
									nState = 4;
									break;
								}
								Sleep(100);
							}

						}


					}
				}
				else
				{
					m_dm->KeyPressChar(L"i");
					Sleep(RandomNumber(1000, 1600));
				}

				for (size_t i = 0; i < 5; i++)
				{
					Sleep(500);
					int nColNum = m_dm->GetColorNum(200, 569, 814, 670, L"e0cfaa-050505", 0.95);
					if (nColNum > 800) {
						if (m_dm->RunScript(L"背包线")) {
							m_dm->KeyPressChar(L"i");
							Sleep(1500);
						}
						nState = 4;
						break;
					}
				}
			}
			else if (nState == 4) {
				if (m_dm->RunScript(L"扎昆物品")) {
					nState = 5;
				}
				else
				{
					g_main_cwnd->AppendText(L"持续攻击");
					OcrBase ocrInfo = baseMap[L"扎昆物品"];
					ocrInfo.nCount = 1;
					for (size_t i = 0; i < RandomNumber(55, 70); i++)
					{
						m_dm->KeyDown(16);
						Sleep(RandomNumber(80, 120));
						m_dm->KeyUp(16);
						//m_dm->RunScript()
						if (m_dm->RunScript(ocrInfo)) {
							break;
						}
						//g_main_cwnd->AppendText(L"攻击中");
					}
				}
				
			}
			else if (nState == 5) {
				for (int k=0;k<2;k++)
				{
					m_dm->KeyDown(37);
					Sleep(RandomNumber(100, 200));

					for (size_t i = 0; i < RandomNumber(45, 50); i++)
					{
						m_dm->KeyDown(90);
						Sleep(RandomNumber(80, 120));
						m_dm->KeyUp(90);
					}
					m_dm->KeyUp(37);
					m_dm->KeyUp(90);
					Sleep(RandomNumber(400, 600));

					m_dm->KeyDown(39);
					Sleep(RandomNumber(100, 200));
					for(size_t i = 0; i < RandomNumber(50, 55); i++)
					{
						m_dm->KeyDown(90);
						Sleep(RandomNumber(80, 120));
						m_dm->KeyUp(90);
					}
					Sleep(RandomNumber(1300, 1500));
					m_dm->KeyUp(39);
					m_dm->KeyUp(90);
				}
				nState = 6;
			}
			else if (nState == 6) {
				if (m_dm->RunScript(L"扎昆物品")) {
					nState = 5;
				}
				ExitZkjt();
				ExitZkrk();
				if (m_dm->RunScript(L"扎昆祭台地图")) {
					Sleep(500);
					m_dm->KeyPress(32);
					Sleep(500);
				}
				m_dm->RunScript(L"退出确定");
				Sleep(500);
				m_dm->RunScript(L"退出确定1");
				if (m_dm->RunScript(L"村庄地图")) {
					m_State = 7;
				}
			}
		}
		else if (m_State == 7) {//Boss已完成
			SellEquip();
		}
		else if (m_State == 8) {
			if (m_dm->RunScript(L"红电脑")) {
				m_State = 9;
				continue;
			}
			if (m_dm->RunScript(L"交易地图")) {
				if (!bStamp && g_main_cwnd->m_zhaKunYou.GetCheck() == 1) {
					bStamp = ZkYouPiao();
				}
				Trade();
			}
			else {
				Sleep(RandomNumber(80, 120));
				m_dm->FindPic(119, 52, 732, 294, L"bTag.bmp", L"202020", 0.90, 0, &x, &y);
				if (x == -1)
				{
					m_dm->KeyPress(66);
				}
				else {
					m_dm->MoveTo(x + 20, y + 3);
					m_dm->LeftClick();
					Sleep(RandomNumber(80, 120));
				}
			}
			
		}
		else if (m_State == 9) {
			//执行下一角色 
			uNum = 0;
			ChangeState();
		}

	}
}

//进入扎昆组队
void CScriptMapleZk::AddTeam()
{
	long x, y;
	CString strXue = m_dm->FindImgEx(L"血条");
	if (strXue.Find(L"|") > 0) {//多个血条
		CloseTearm();//退出队伍
	}
	if (strXue == L"") {
		m_dm->KeyPressChar(L"p");
		Sleep(RandomNumber(500, 800));
		if (m_dm->RunScript(L"导航窗口", &x, &y)) {
			//m_dm->RunScript(L"添加组队");
			m_dm->MoveTo(x - 30 + RandomNumber(10, 20), y + 330 + RandomNumber(1, 10));
			Sleep(RandomNumber(50, 150));
			m_dm->LeftClick();
			Sleep(RandomNumber(500, 1500));
			m_dm->RunScript(L"确定组队");
		}
	}
}

//卖装备
void CScriptMapleZk::SellEquip()
{
	long x, y,x1,y1;
	bool bState = false;//是否已经清理完成 false未完成 true 完成
	bool bFlag = false;//武器未清空
	for (size_t i = 0; i < 15; i++)
	{
		if (!bState) {//未清理
			//是否到达自由市场
			if (m_dm->RunScript(L"自由市场")) {
				if (m_dm->RunScript(L"清理装备窗口", &x1, &y1)) {
					if (m_dm->RunScript(L"自由市场其他栏")) {//选中其他栏目
						for (size_t i = 0; i < 5; i++)
						{
							if (m_dm->RunScript(L"物品球")) {
								Sleep(RandomNumber(1000, 1500));
								m_dm->KeyPressChar(L"enter");
								
							}
							Sleep(RandomNumber(500, 1000));
						}
						bState = true;
						continue;
					}
					if (m_dm->RunScript(L"装备清空")) {//装备清空执行选中其他栏
						Sleep(RandomNumber(1000, 2000));
						m_dm->MoveToEx(x1 + 250, y1 + 98, 5, 5); //选中其他栏目清理
						Sleep(RandomNumber(200, 300));
						m_dm->LeftClick();
						Sleep(RandomNumber(200, 300));
						m_dm->MoveToEx(x1 , y1, 5, 45);
						bFlag = true;
					}
					else {//未找到装备栏
						//选中装备
						//确定卖掉
						if (bFlag)continue;
						Sleep(RandomNumber(1000, 2000));
						m_dm->MoveToEx(x1 + 253, y1 + 125, 20, 10);//选中武器
						m_dm->LeftClick();
						Sleep(RandomNumber(500, 1300));
						long nRndX = RandomNumber(1, 15);
						long nRndY = RandomNumber(1, 5);
						m_dm->MoveTo(x1 + 500, y1 + RandomNumber(200, 300));
						for (size_t k = 0; k < 10; k++)
						{
							if (m_dm->RunScript(L"选中装备")) {
								m_dm->MoveTo(x1 + 330 + nRndX, y1 + 67 + nRndY);
								m_dm->LeftClick();
								Sleep(RandomNumber(500, 1000));
								if (m_dm->RunScript(L"确定"))
								{
									m_dm->KeyPressChar(L"enter");
									Sleep(RandomNumber(100, 200));
								}
							}

						}
					}
				}
				else
				{
					m_dm->RunScript(L"npc收藏家");
				}
			}
			else {
				//导航到自由市场
				if (m_dm->RunScript(L"村庄地图",&x,&y)) {
					m_dm->MoveToEx(x + 10, y + 10, 10, 5);
					m_dm->LeftClick();
					Sleep(RandomNumber(550, 800));
					m_dm->MoveToEx(x + 220, y + 300, 10, 35);
					if (m_dm->RunScript(L"快速导航", &x, &y)) {
						Sleep(RandomNumber(550, 800));
						m_dm->MoveToEx(x - 30, y + 70, 10, 5);
						Sleep(RandomNumber(150, 200));
						m_dm->LeftClick();
						Sleep(RandomNumber(150, 200));
					}
					m_dm->RunScript(L"退出确定");
					Sleep(500);
					m_dm->RunScript(L"退出确定1");
				}
			}
			
		}
		else
		{
			if (m_dm->RunScript(L"清理装备窗口")) {
				m_dm->KeyPressChar(L"esc");
				Sleep(500);
			}
			else {
				ExitShiChang();
			}
			if (m_dm->RunScript(L"村庄地图")) {
				m_State = 8;
			}
		}
	}
	if (m_dm->RunScript(L"村庄地图")) {
		m_State = 8;
	}

						
}

//交易
void CScriptMapleZk::Trade()
{
	long x, y;
	m_dm->SetDict(0, L"jinbi.txt");
	m_dm->UseDict(0);
	//bool bStamp = false;
	UINT nState = 0;
	CString strWupin = L"";
	bool bFlag = false;
	while (m_Flag)
	{
		//是否换邮票

		switch (nState)
		{
		case 0:
			//切换线路
			nState = LineSwitching();
			break;
		case 1:
			bFlag = DropItem(strWupin);
			if (strWupin == L"" && bFlag) {
				nState = 2;
			}
			else {
				if (SuppLies(strWupin)) nState = 2;
			}
			break;
		}
		if (nState ==2)
		{
			if (Trading())break;
		}
	}
}

void CScriptMapleZk::ChangeState()
{
	int nRoleNum = g_AccountFile.GetInt(NumberToString(m_Index + 1), L"RoleNum");
	++nRoleNum;
	g_AccountFile.SetInt(NumberToString(m_Index + 1), L"RoleNum", nRoleNum);
	g_main_cwnd->m_list_info.SetItemText(m_Index, 7, NumberToString(nRoleNum));
	g_main_cwnd->AppendText(L"当前状态:" + NumberToString(m_State));
	//当前进行的角色+1 如果等于进行的总数则切换账号
	//int nRoleNum = g_AccountFile.GetInt(NumberToString(m_Index + 1), L"RoleNum");
	int nZhaNum = g_AccountFile.GetInt(NumberToString(m_Index + 1), L"ZhaNum");
	if (nRoleNum >= nZhaNum) {//切换账号
							  /*g_bSwicth=true;
							  g_main_cwnd->m_list_info.SetItemText(taskIndex, 6, L"完成");
							  g_AccountFile.SetString(NumberToString(taskIndex + 1), L"state", L"1");*/

		g_main_cwnd->m_list_info.SetItemText(m_Index, 6, L"完成");//当前账号置完成
		m_Index = m_Index + 1;
		g_AccountFile.SetString(NumberToString(m_Index), L"state", L"1");

		int nLineCode = g_IniFile.GetInt(L"Set", L"CodeLine");//当前配置线路
		int nLineCode1 = _wtoi(g_main_cwnd->m_list_info.GetItemText(m_Index, 12));
		if (nLineCode == nLineCode1) {
			//切换角色
			SwicthRole(true, true);
		}
		else {
			m_Flag = false;
			return;
		}
	}
	else {
		SwicthRole(true, false);
		m_State = 0;
		//g_main_cwnd->AppendText(L"当前状态:" + NumberToString(g_nState));
	}

}

//检查游戏状态
void CScriptMapleZk::CheckGameState()
{
	if (m_SubDm != NULL) {
		int nTime = GetTickCount();
		if (m_SubDm->IsDisplayDead(0, 760, 14, 769, 1) == 1) {
			//nCountTime++;
			m_Time = m_Time + (GetTickCount() - nTime) / 1000;
		}
		else {
			m_Time = 0;
		}
		if (m_Time > 380) {
			CString str;
			str = g_main_cwnd->m_list_info.GetItemText(m_Index, 1) + g_main_cwnd->m_list_info.GetItemText(m_Index, 7);
			m_SubDm->CaptureGif(0, 0, 1280, 960, L"d:\\pic\\" + str + L".gif", 100, 10000);
			m_State = 0;
			g_IniFile.SetString(NumberToString(m_Index + 1), L"RoleCode", NumberToString(m_Index) + L"," + g_main_cwnd->m_list_info.GetItemText(m_Index, 7));
			m_Time = 0;
			KillProcess(L"MapleStory.exe");
			m_Flag = false;
		}
		g_main_cwnd->AppendText(L"当前状态:" + NumberToString(m_State)+L"-当前卡屏时间："+NumberToString(m_Time));
	}
}

bool CScriptMapleZk::ZkYouPiao()
{
	long x, y, x1, y1;
	bool bDrop = false;
	m_dm->UseDict(0);
	for (size_t i = 0; i < 10; i++)
	{
		//1.按键n
		//Sleep(RandomNumber(500, 1000));
		if (m_dm->RunScript(L"猎人窗口", &x, &y)) {
			//是否选中商店栏目n
			if (m_dm->RunScript(L"猎人商店栏目")) {
				//long x1, y1;
				if (m_dm->RunScript(L"确定", &x1, &y1)) {

					//识别数字
					CString strNum = m_dm->Ocr(x1, y1 - 70, x1 + 100, y1, L"ffffff", 1);
					if (strNum == L"" || strNum == L"0") {
						Sleep(RandomNumber(500, 1000));
						if (m_dm->RunScript(L"确定")) {
							m_dm->KeyPress(27);
						}
						Sleep(RandomNumber(500, 1000));
						if (m_dm->RunScript(L"猎人窗口")) {
							m_dm->KeyPress(27);
						}
						return true;
					}
					m_dm->SendString2(m_dm->GetBindWindow(), strNum);
					Sleep(RandomNumber(500, 1000));
					m_dm->KeyPress(13);
					Sleep(RandomNumber(1500, 2000));
					if (m_dm->RunScript(L"确定")) {
						m_dm->KeyPress(13);
						Sleep(RandomNumber(1500, 2000));
						return true;
					}

				}
				else {
					m_dm->Click(x, y + 56, 30, 5);
					Sleep(RandomNumber(80, 150));
					m_dm->LeftClick();
					Sleep(RandomNumber(500, 1000));
				}
			}
			else {
				m_dm->Click(x + 152, y + 25, 10, 3);
				Sleep(RandomNumber(500, 1000));
			}
		}
		else {
			m_dm->KeyPressChar(L"n");
			Sleep(RandomNumber(500, 1000));
			if (!m_dm->RunScript(L"猎人窗口")) {//未打卡十字猎人窗口
				for (size_t k = 0; k < 10; k++)
				{
					if (m_dm->RunScript(L"猎人N快捷键")) {
						m_dm->RunScript(L"快捷键确认");
						break;
					}


					if (m_dm->RunScript(L"初始化快捷键", &x, &y))//是否打开快捷键窗口
					{
						//+235 -247
						//long x1, y1;
						if (m_dm->RunScript(L"猎人按钮", &x1, &y1)) {
							//if (y1 > y - 177) {//猎人按钮未设定快捷键
							//	
							//}
							m_dm->Click(x1 + 5, y1 - 5, 5, 5);
							Sleep(RandomNumber(1000, 2000));
							m_dm->MoveToEx(x + 245, y - 237, 5, 5);
							Sleep(RandomNumber(1000, 2000));
							m_dm->LeftClick();
							Sleep(RandomNumber(500, 2000));
							m_dm->MoveToEx(5, 5, 200, 500);
						}
					}
					else {//未打开快捷键窗口执行快捷键窗口
						if (m_dm->RunScript(L"设置", &x, &y)) {//设置是否打开
							m_dm->Click(x + 10, y + 117, 10, 5);
							Sleep(RandomNumber(1000, 2000));
						}
						else {
							m_dm->KeyPressChar(L"esc");
						}
					}
				}
			}
		}
	}
	return true;
	
}

void CScriptMapleZk::SetNextRole()
{
	m_State = 9;
}
